package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.soms.dto.FollowTypeDto;
import com.dingreading.cloud.soms.entity.FollowType;
import com.dingreading.cloud.soms.entity.table.FollowTypeTableDef;
import com.dingreading.cloud.soms.mapper.FollowTypeMapper;
import com.dingreading.cloud.soms.service.FollowTypeService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class FollowTypeServiceImpl extends BaseServiceImpl<FollowTypeMapper, FollowType, FollowTypeTableDef> implements FollowTypeService {

    @Override
    protected FollowTypeTableDef getTable() {
        return FollowTypeTableDef.followType;
    }

    @Override
    public long getHaveName(String storeUid, String uid, String name) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.isNull());
        condition.and(table.name.eq(name));

        QueryCondition condition2 = QueryCondition.createEmpty();
        condition2.and(table.storeUid.eq(storeUid));
        if (StringUtils.isNotBlank(uid))
            condition2.and(table.uid.ne(uid));
        condition2.and(table.name.eq(name));

        return getMapper().selectCountByCondition(condition);
    }


    @Override
    public FollowType getBean(String storeUid, String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.uid.eq(uid));
        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public int dropBean(String storeUid, String uid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid));
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        return getMapper().deleteByCondition(condition);
    }

    @Override
    public List<FollowTypeDto> getByStoreUid(String storeUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.isNull().and(table.enabled.eq(1)))
                .or(table.storeUid.eq(storeUid).and(table.enabled.eq(1)));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.handStatus.asc(), table.id.asc());

        return getMapper().selectListByQueryAs(wrapper, FollowTypeDto.class);
    }

    @Override
    public List<FollowTypeDto> getStoreFollowType(String storeUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid).or(table.storeUid.isNull()))
                .and(table.enabled.eq(1))
                .and(table.handStatus.eq(1));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.id.asc());

        return getMapper().selectListByQueryAs(wrapper, FollowTypeDto.class);
    }

    @Override
    public Page<FollowTypeDto> pageList(PageUtil pageUtil, String storeUid, String keyword, Integer enabled) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.isNull().and(table.enabled.eq(1))
                .or(table.storeUid.eq(storeUid))
        );
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.name.like(keyword));
        if (null != enabled)
            condition.and(table.enabled.eq(enabled));

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.handStatus.asc(), table.id.asc());

        return getMapper().paginateAs(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, FollowTypeDto.class);
    }

}
